//
//  LGScreenViewController.m
//  LuxuryGoods
//
//  Created by 纳里健康 on 2018/6/7.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGScreenViewController.h"
#import "CollTextFieldCell.h"
#import "CollCell.h"
#import "AttributeHeadView.h"


@interface LGScreenViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_screenCollection;
    
    NSMutableArray *_dataArray;
    
    NSArray *_titleArray;
    
    UICollectionReusableView *_footerView;
}

@property (nonatomic, copy) NSString *maxPrice;
@property (nonatomic, copy) NSString *mixPrice;

@property (nonatomic, assign) NSInteger selectCategoryIndex;



@end

@implementation LGScreenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _titleArray = @[@"品牌",@"品类",@"品类",@"降价/分期"];
    
    self.view.backgroundColor = [UIColor colorWithWhite:.0f alpha:0.5];
    
    [self makeCollectionView];
}

- (void)makeBottomBtn
{
    _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _screenCollection.width, 50)];
    
    UIButton *button = [self makeButtonWithTitle:@"重置" titleColor:UIColorFromRGB(0x666666) backGroundColor:WHITE_COLOR action:@selector(chongZhiAction)];
    [_footerView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.centerY.equalTo(self->_footerView);
        make.size.mas_equalTo(CGSizeMake((self->_footerView.width - 18 * 2)/2, 35));
    }];
    
    UIButton *sureBtn = [self makeButtonWithTitle:@"确定" titleColor:WHITE_COLOR backGroundColor:UIColorFromRGB(0xC0B48E) action:@selector(sureAction)];
    [_footerView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-18);
        make.centerY.equalTo(self->_footerView);
        make.size.mas_equalTo(CGSizeMake((self->_footerView.width - 18 * 2)/2, 35));
    }];
    
}

- (UIButton *)makeButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor backGroundColor:(UIColor *)backColor action:(SEL)btnAction
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setBackgroundColor:backColor];
    [btn addTarget:self action:btnAction forControlEvents:UIControlEventTouchUpInside];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    btn.layer.borderColor = UIColorFromRGB(0xD1C8AE).CGColor;
    btn.layer.cornerRadius = 17.0f;
    btn.layer.masksToBounds = YES;
    
    return btn;
}


- (void)makeCollectionView
{
    CGFloat collectionWidth = SCREEN_WIDTH - 30;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置滑动方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    flowLayout.minimumLineSpacing = 5;
    
    flowLayout.minimumInteritemSpacing = 5;
    
    _screenCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(30, 0, collectionWidth, SCREEN_HEIGHT - NAVIGATOR_HEIGHT) collectionViewLayout:flowLayout];
    
    _screenCollection.backgroundColor = [UIColor whiteColor];
    
    _screenCollection.delegate = self;
    
    _screenCollection.dataSource = self;
    
    _screenCollection.scrollEnabled = YES;
    
    [self.view addSubview:_screenCollection];
    
    [_screenCollection registerNib:[UINib nibWithNibName:@"CollTextFieldCell" bundle:nil] forCellWithReuseIdentifier:@"CollTextFieldCell"];
    
    [_screenCollection registerNib:[UINib nibWithNibName:@"CollCell" bundle:nil] forCellWithReuseIdentifier:@"CollCell"];
    
    [_screenCollection registerNib:[UINib nibWithNibName:@"AttributeHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"AttributeHeadView"];
    
    [_screenCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter"];
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return section == 0 ? 1 : 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        CollTextFieldCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollTextFieldCell" forIndexPath:indexPath];
        
        cell.numberBlock = ^(NSString *min, NSString *max) {
            
            self.mixPrice = min;
            self.maxPrice= max;
            [self->_screenCollection reloadData];
        };
        
        return cell;

    }else
    {
        CollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollCell" forIndexPath:indexPath];
        
        if (self.selectCategoryIndex == indexPath.row) {
            
            cell.layer.borderWidth = 0;
            cell.nameLabel.textColor = WHITE_COLOR;
            cell.nameLabel.backgroundColor = UIColorFromRGB(0xC0B48E);
            
        }else{
            
            cell.layer.borderWidth = 0.5;
            cell.nameLabel.textColor = UIColorFromRGB(0x666666);
            cell.nameLabel.backgroundColor = UIColorFromRGB(0xEEEEEE);
        }
        return cell;
    }
    
    return  nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return  CGSizeMake(_screenCollection.width, 90);
        
    }else{
        
        CGFloat itemWidth = ceilf(_screenCollection.width - 15 * 3)/4;
        
        return  CGSizeMake(itemWidth, 28);
        
    }
}

#pragma mark  定义整个CollectionViewCell的四个边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return  UIEdgeInsetsMake(0, 0, 0, 0);
    }else{
        return  UIEdgeInsetsMake(10, 10, 10, 10);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return CGSizeMake(0, 0);
        
    }else{
        return CGSizeMake(_screenCollection.width, 30);
    }
}

//返回头部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        if (indexPath.section == 0) {
            
            return nil;
            
        }else{
            
            AttributeHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"AttributeHeadView" forIndexPath:indexPath];
            
            if (headerView) {
                [headerView removeFromSuperview];
            }
            
            headerView.leftLabel.text = _titleArray[indexPath.section - 1];
            
            return headerView;
        }
    }else if([kind isEqualToString:UICollectionElementKindSectionFooter])
    {
        if(indexPath.section == _titleArray.count)
        {
            UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"collectionFooter" forIndexPath:indexPath];
            
            [self makeBottomBtn];
            
            [footerView addSubview:_footerView];
            
            return footerView;
            
        }
    }
    return nil;
}

//重置按钮
- (void)chongZhiAction
{
    
}

- (void)sureAction
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
